package com.thinkpadteam.edugra.game;

import java.util.List;

import com.thinkpadteam.edugra.MainActivity;
import com.thinkpadteam.edugra.MainGameActivity;
import com.thinkpadteam.edugra.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GameBerhitung extends Activity {

	private int SOAL=0, SKOR=0;
	TextView text_soal;
	ImageButton img_soal, pil1, pil2,pil3, home;
	Cursor c=null;
	List<Soal> listSoalBerhitung;
	Soal soal;
	SQLiteConnector sqlConnect;
	Boolean cek=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_game_berhitung);
        Typeface tf = Typeface.createFromAsset(getAssets(),"HugMeTight.ttf");
		
		text_soal = (TextView) findViewById(R.id.tv_desc_soal_berhitung);
		text_soal.setTypeface(tf);
		img_soal = (ImageButton) findViewById(R.id.iv_soal_berhitung);
		pil1 = (ImageButton) findViewById(R.id.iv_pil1_berhitung);
		pil2 = (ImageButton) findViewById(R.id.iv_pil2_berhitung);
		pil3 = (ImageButton) findViewById(R.id.iv_pil3_berhitung);
		home = (ImageButton) findViewById(R.id.homeberhitung);
		home.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GameBerhitung.this, MainActivity.class);
				startActivity(intent);
			}
		});


		sqlConnect = new SQLiteConnector(this);
		
		String called_from = getIntent().getStringExtra("kategori");
//		if(called_from.equalsIgnoreCase("kosong")){
			listSoalBerhitung= sqlConnect.getAllRecord(2);
//			for(int i=1; i>=0; i--){
//				Log.d("ini caranya log", listSoal.get(i).getDesc());
//			}
//		}else{
//			listSoal= sqlConnect.getAllRecord();
//		}
			Toast.makeText(GameBerhitung.this, called_from, Toast.LENGTH_SHORT).show();
		text_soal.setText(listSoalBerhitung.get(0).getDesc());
		
		this.setBackg();
		
	}
	
	public void setBackg()
	{
		if(SOAL < listSoalBerhitung.size()){
//			Toast.makeText(GameMencocokkan.this, "soal ke- "+SOAL, Toast.LENGTH_SHORT).show();
			text_soal.setText(listSoalBerhitung.get(SOAL).getDesc());
			img_soal.setBackground(stringToDrawable(listSoalBerhitung.get(SOAL).getPath()));	
			pil1.setBackground(stringToDrawable(listSoalBerhitung.get(SOAL).getPilihan1()));
			pil2.setBackground(stringToDrawable(listSoalBerhitung.get(SOAL).getPilihan2()));
			pil3.setBackground(stringToDrawable(listSoalBerhitung.get(SOAL).getPilihan3()));	
//			Toast.makeText(GameMencocokkan.this, listSoal.size()+" isi listsoal", Toast.LENGTH_SHORT).show();			
		}else{
			Toast.makeText(GameBerhitung.this, "SELESAI", Toast.LENGTH_SHORT).show();					
			Intent intent = new Intent(GameBerhitung.this, MainGameActivity.class);
			intent.putExtra("skor", "Skor = "+SKOR);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);			
			startActivity(intent);
		}
	}
	
	public Drawable stringToDrawable(String name){
		int id = getResources().getIdentifier(name, "drawable", getPackageName());
		Drawable drawable = getResources().getDrawable(id);
		return drawable;
	}
	
	public void cekjwbnberhitung(View v){
		Intent intent;
		switch (v.getId()) {
		case R.id.iv_pil1_berhitung:
			if(listSoalBerhitung.get(SOAL).getPilihan1().equals(listSoalBerhitung.get(SOAL).getJawaban())){
				SKOR++;
				Toast.makeText(GameBerhitung.this, "BENAR "+SKOR, Toast.LENGTH_SHORT).show();
				SOAL++; 
				customToast("benar");
				setBackg();
			}
			else{
				Toast.makeText(GameBerhitung.this, "SALAH "+SKOR, Toast.LENGTH_SHORT).show();				
				SKOR--;
				customToast("salah");}
			break;
		case R.id.iv_pil2_berhitung :
			if(listSoalBerhitung.get(SOAL).getPilihan2().equals(listSoalBerhitung.get(SOAL).getJawaban())){
				SKOR++;
				customToast("benar");
				Toast.makeText(GameBerhitung.this, "BENAR "+SKOR, Toast.LENGTH_SHORT).show();
				SOAL++; 
				setBackg();
			}
			else{
				Toast.makeText(GameBerhitung.this, "SALAH "+SKOR, Toast.LENGTH_SHORT).show();				
				SKOR--;
				customToast("salah");}
			break;
		case R.id.iv_pil3_berhitung :
			if(listSoalBerhitung.get(SOAL).getPilihan3().equals(listSoalBerhitung.get(SOAL).getJawaban())){
				SKOR++;
				customToast("benar");
				Toast.makeText(GameBerhitung.this, "BENAR "+SKOR, Toast.LENGTH_SHORT).show();
				SOAL++; 
				setBackg();
			}
			else{
				Toast.makeText(GameBerhitung.this, "SALAH "+SKOR, Toast.LENGTH_SHORT).show();				
				SKOR--;
				customToast("salah");}
			break;

		default:
			break;
		}
		Log.d("skor", "skor== "+SKOR);
		Log.d("soal", "soal== "+SOAL);
	}
	
	public void onBackPressed()
	{
        Intent intent = new Intent(this, MainGameActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
	}
	
	public void customToast(String answer){
		// get your custom_toast.xml ayout
		LayoutInflater inflater = getLayoutInflater();

		View layout = inflater.inflate(R.layout.layout_custom_toast,
				(ViewGroup) findViewById(R.id.custom_toast_layout_id));

		// set a dummy image
		ImageView image = (ImageView) layout.findViewById(R.id.image);
		if(answer.equalsIgnoreCase("benar")){
			image.setImageResource(R.drawable.toast_benar);			
		}else{
			image.setImageResource(R.drawable.toast_salah);
		}


		// Toast...
		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();		
	}	
}
